package com.crm.controller.client;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.pojo.ClientInfo;
import com.crm.service.client.ContactService;
import com.crm.service.client.MemorialService;
import com.crm.vo.ContactIdAndNameVo;
import com.crm.vo.MemorialVo;
import com.crm.vo.Result;

@Controller
public class MemorialController {

	@Autowired
	private MemorialService memorialService;
	@Autowired
	private ContactService contactService;
	/**
	 * 转到客户纪念日页面
	 * @return
	 */
	@GetMapping("/to-client-memorial")
	public String toClientMemorial() {
		return "client/client-memorial";
	}
	/**
	 * 转到纪念日表单页面
	 * @return
	 */
	@GetMapping("/to-memorial-form")
	public String toMemorialForm() {
		return "client/memorial-form";
	}
	
	/**
	 * 转到编辑纪念日页面
	 * @return
	 */
	@GetMapping("/edit-memorial-form")
	public String editMemorialForm() {
		return "client/edit-memorial-form";
	}
	
	/**
	 * 纪念日视图页面
	 * @return
	 */
	@GetMapping("/view-memorial-form")
	public String viewMemorialForm() {
		return "client/view-memorial-form";
	}
	
	/**
	 * 查询所有的纪念日
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/query-all-memorial")
	@ResponseBody
	public Map queryAllMemorial(int page, int rows)  {
		Long total = memorialService.queryCountAllMemorial();
		List<MemorialVo> memorialVos = memorialService.queryAllMemorial(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", memorialVos);
		return map;
	}
	/**
	 * 根据id查询所有的纪念日
	 * @param memId
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/query-memorial-by-memId")
	@ResponseBody
	public Map queryMemorialByMemId(Long memId,int page,int rows) {
		Long total = memorialService.queryCountMemorialByMemId(memId);
		List<MemorialVo> memorialVo = memorialService.queryMemorialByMemId(memId, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", memorialVo);
		return map;
	}
	/**
	 * 根据联系人姓名模糊查询纪念日
	 * @param contactName
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/query-memorial-by-contactName")
	@ResponseBody
	public Map queryMemorialByContactName(String contactName, int page, int rows) {
		Long total = memorialService.queryCountMemorialByContactName(contactName);
		List<MemorialVo> memorialVos = memorialService.queryMemorialByContactName(contactName, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", memorialVos);
		return map;
	}
	
	/**
	 * 查询本月的纪念日
	 * @return
	 */
	@PostMapping("/query-memorial-curMonth")
	@ResponseBody
	public List<MemorialVo> queryMemorialCurMonth(){
		return memorialService.queryMemorialCurMonth();
	}
	
	/**
	 * 查询下个月的纪念日
	 * @return
	 */
	@PostMapping("/query-memorial-nextMonth")
	@ResponseBody
	public List<MemorialVo> queryMemorialNextMonth() {
		return memorialService.queryMemorialNextMonth();
	}
	
	/**
	 * 查询联系人的编号和姓名
	 * @return
	 */
	@PostMapping("/memorial-find-contact-id-name")
	@ResponseBody
	public List<ContactIdAndNameVo> findContactIdAndName() {
		return contactService.findContactIdAndName();
	}
	
	/**
	 * 通过客户编号查询联系人的编号和姓名
	 * @param cid
	 * @return
	 */
	@PostMapping("/memorial-find-contact-id-name-by-cid")
	@ResponseBody
	public List<ContactIdAndNameVo> findContactIdAndNameById(Long cid) {
		return contactService.findContactIdAndNameByCid(cid);
	}
	/**
	 * 通过联系人编号查询客户
	 * @param contactid
	 * @return
	 */
	@PostMapping("/memorial-find-clientInfo-by-contactid")
	@ResponseBody
	public List<ClientInfo>  findClientInfoBycontactid(Long contactid) {
		return contactService.findClientInfoBycontactid(contactid);
	}
	/**
	 * 新增联系人纪念日
	 * @param contactid
	 * @param type
	 * @param memDate
	 * @param remarks
	 * @return
	 */
	@PostMapping("/add-memorial")
	@ResponseBody
	public Result addMemorial(Long memId, BigDecimal contactid, String type, Date memDate, String remarks) {
		return memorialService.addMemorial(memId, contactid, type, memDate, remarks);
	}
	
	@PostMapping("/del-memorial")
	@ResponseBody
	public Result delMemorial(Long memId) {
		return memorialService.delMemorial(memId);
	}
}
